using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using OrderService.Application.Dtos;
using OrderService.Application.Services;

namespace OrderService.Controllers
{
    /// <summary>
    /// 订单控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class OrderController : ControllerBase
    {
        private readonly IOrderAppService _orderAppService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="orderAppService">订单应用服务</param>
        public OrderController(IOrderAppService orderAppService)
        {
            _orderAppService = orderAppService;
        }

        /// <summary>
        /// 创建订单
        /// </summary>
        /// <param name="request">创建订单请求</param>
        /// <returns>创建的订单ID</returns>
        [HttpPost]
        public async Task<ActionResult<Guid>> CreateOrder([FromBody] CreateOrderRequest request)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            var orderId = await _orderAppService.CreateOrderAsync(request);
            return CreatedAtAction(nameof(GetOrder), new { id = orderId }, orderId);
        }

        /// <summary>
        /// 获取订单详情
        /// </summary>
        /// <param name="id">订单ID</param>
        /// <returns>订单信息</returns>
        [HttpGet("{id}")]
        public async Task<ActionResult<OrderResponse>> GetOrder(Guid id)
        {
            var order = await _orderAppService.GetOrderAsync(id);
            if (order == null)
                return NotFound();

            return Ok(order);
        }

        /// <summary>
        /// 根据订单编号获取订单
        /// </summary>
        /// <param name="orderNumber">订单编号</param>
        /// <returns>订单信息</returns>
        [HttpGet("number/{orderNumber}")]
        public async Task<ActionResult<OrderResponse>> GetOrderByNumber(string orderNumber)
        {
            var order = await _orderAppService.GetOrderByNumberAsync(orderNumber);
            if (order == null)
                return NotFound();

            return Ok(order);
        }

        /// <summary>
        /// 根据客户ID获取订单列表
        /// </summary>
        /// <param name="customerId">客户ID</param>
        /// <returns>订单列表</returns>
        [HttpGet("customer/{customerId}")]
        public async Task<ActionResult<List<OrderResponse>>> GetOrdersByCustomer(Guid customerId)
        {
            var orders = await _orderAppService.GetOrdersByCustomerAsync(customerId);
            return Ok(orders);
        }

        /// <summary>
        /// 更新订单状态
        /// </summary>
        /// <param name="id">订单ID</param>
        /// <param name="request">更新订单状态请求</param>
        [HttpPut("{id}/status")]
        public async Task<IActionResult> UpdateOrderStatus(Guid id, [FromBody] UpdateOrderStatusRequest request)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            await _orderAppService.UpdateOrderStatusAsync(id, request.Status, request.Remark);
            return NoContent();
        }

        /// <summary>
        /// 取消订单
        /// </summary>
        /// <param name="id">订单ID</param>
        [HttpPost("{id}/cancel")]
        public async Task<IActionResult> CancelOrder(Guid id)
        {
            await _orderAppService.CancelOrderAsync(id);
            return NoContent();
        }
    }
}